/*Generate Tables in the main text and the appendix*/
clear all
set more off

global processed_data        "${rootdir}\processed_data"
global density_data_dir      "${rootdir}\results\density\bin_data"
global density_graph_dir     "${rootdir}\results\density\bunching_figs"
global density_mse_dir       "${rootdir}\results\density\mse"
global causal_data_dir       "${rootdir}\results\causal\reg_data"  
global causal_temp_dir       "${rootdir}\results\causal\reg_data\temp"  
global causal_bt_dir         "${rootdir}\results\causal\bootstrap" 
global causal_ITT_dir        "${rootdir}\results\causal\TE"  
global causal_graph_dir      "${rootdir}\results\causal\outcome_figs"
cd "${rootdir}\code"


qui run "${rootdir}\code\sub_create.do"
qui run "${rootdir}\code\sub_density_nonparametric.do"
qui run "${rootdir}\code\sub_density_parametric_compute.do"
qui run "${rootdir}\code\sub_density_parametric_compute_fie.do"
qui run "${rootdir}\code\sub_density_parametric_search.do"
qui run "${rootdir}\code\sub_figure_chen.do"
qui run "${rootdir}\code\sub_causal.do" 
qui run "${rootdir}\code\sub_causal_2sls.do" 
qui run "${rootdir}\code\sub_causal_figure.do" 
qui run "${rootdir}\code\notch_mse_roundq.ado"
qui run "${rootdir}\code\notch_ct_roundq.ado"
qui run "${rootdir}\code\notch_fie_ct_roundq.ado"






//Table A1  
use "${processed_data}/trading_rights_asif.dta", clear
**Panel A
tabstat trade_custom export_custom import_custom tfpq if cic_adj2>=13 & cic_adj2<=42,  by(year) stat(mean) format(%5.0g)
tabstat RC   if cic_adj2>=13 & cic_adj2<=42, by(year) stat(p25 p50 p75 )	 format(%7.2g)	
**Panel B
tabstat trade_custom export_custom import_custom tfpq if cic_adj2>=13 & cic_adj2<=42 & year==2002 & (group==1 | group==2), by(group) stat(mean) format(%5.0g)
tabstat RC  if  cic_adj2>=13 & cic_adj2<=42 & year==2002 & (group==1 | group==2), by(group) stat(p25 p50 p75) format(%7.2g)		





/******Column (1) of Table 2 & 3 and Tables: 4, 5, G1, A3, A4, A5, C3, C1******/
set matsize 6000
set seed 1234
global draw_fig         =0
global nboot            =500
global run_bootstrap    =1
global reg_degree       "2nd"          
global fe_status        "fe"            
global bs_method        "bparametric"   
global density          "DP_q"  

global cutoff=300 
global condition       "group==1 & cic_adj2>=13 & cic_adj2<=42 "  
global name            "East_Manuf"
global location        "East"
global firm            "domestic"  
global firm_name       "Domestic-Owned"
global year_condition  "year==2002"
global year_name       "2002"
global year_note       "in 2002"
global cutoff_note     "(cutoff=300)"
global degree=5
global low=-70  
global high=10
global graph_low=$cutoff + $low
global graph_high=$cutoff + $high
qui density_data
qui density_parametric_compute 


**Column (1) of Table 2
global winsor=0  
global trim=1   //adjsut when predicted value of dummy variable >1 or <0
global myvar  "trade_custom" 
global myvar_name "Trade Participation"
qui causal_est


**Columns (1) & (4) of Table 3
global winsor=1 
global trim=0
global myvar_name "Productivity"
global myvar "tfpq"
qui causal_est
qui causal_2sls_est   


**Columns (2)-(6) of Tables 4
global winsor=1 
global trim=0

global myvar_name "Productivity in 2002"
global myvar "tfpq"
qui causal_est

global myvar_name "Productivity in 2003"
global myvar "tfpq_F"
qui causal_est

global myvar_name "Productivity in 2004"
global myvar "tfpq_F2"
qui causal_est

global myvar_name "Productivity in 2005"
global myvar "tfpq_F3"
qui causal_est

global myvar_name "Productivity in 2006"
global myvar "tfpq_F4"
qui causal_est


**Table 5 
global winsor=0
global trim=1
global myvar_name "Having R&D"
global myvar "RD_d" 
qui causal_est

global winsor=0
global trim=0

global myvar_name "R&D"
global myvar "RD" 
qui causal_est

global myvar_name "Number of Products"
global myvar "NUM" 
qui causal_est

global myvar_name "Having New Products"
global myvar "New" 
qui causal_est

global myvar_name "Output of New Product"
global myvar "V212" 
qui causal_est

global winsor=1   //winsorize top & bottom 1% for tfpq and markup 
global trim=0 
global myvar_name "Markup"
global myvar "mkpm" 
qui causal_est


**Table G1
global winsor=1
global trim=0

global myvar_name "Employment"
global myvar "l" 
qui causal_est

global myvar_name "Wage"
global myvar "ln_wage_avrg" 
qui causal_est

global myvar_name "employee benefits"
global myvar "benefit_avrg"
qui causal_est  


**Table A3
global winsor=0  
global trim=1   
global myvar  "trade_custom_F" 
global myvar_name "Trade Participation in 2003"
qui causal_est

global myvar  "trade_custom_F2" 
global myvar_name "Trade Participation in 2004"
qui causal_est

global myvar  "trade_custom_F3" 
global myvar_name "Trade Participation in 2005"
qui causal_est

global myvar  "trade_custom_F4" 
global myvar_name "Trade Participation in 2006"
qui causal_est


**Table A4  
global winsor=0  
global trim=0   

global myvar  "F318"
global myvar_name "Total Asset"
qui causal_est

global myvar  "F322"
global myvar_name "Total Debt"
qui causal_est

global myvar  "F323"
global myvar_name "Equity"
qui causal_est


**Table A5  
global winsor=0  
global trim=0   
global myvar  "F319"
global myvar_name "Short-Term Debt"
qui causal_est

global myvar  "F321"
global myvar_name "Long-Term Debt"
qui causal_est

global myvar_name  "Current assets (total)"
global myvar "F304"
qui causal_est

global myvar  "F314"
global myvar_name "Fixed Asset (net value)"
qui causal_est

global myvar  "F317"
global myvar_name "Intangible Asset"
qui causal_est

global myvar  "F305"
global myvar_name "Inventory"
qui causal_est

global myvar  "F390"
global myvar_name "Receivables (net value)"
qui causal_est

global myvar  "remain_current_ast"
global myvar_name "Other Current Assets"
qui causal_est


**Table C3  
global winsor=0    
global trim=1     
global myvar  "exports_all_d"  
global myvar_name "Trade Participation via ASIF"
qui causal_est


**Table C1 
global degree=5
global low=-80  
global high=20
global graph_low=$cutoff + $low
global graph_high=$cutoff + $high
qui density_parametric_compute 
global winsor=0    
global trim=1      
global myvar  "trade_custom" 
global myvar_name "Trade Participation"
qui causal_est
global winsor=1   
global trim=0      
global myvar  "tfpq" 
global myvar_name "Productivity"
qui causal_est
qui causal_2sls_est


global degree=5
global low=-70  
global high=0
global graph_low=$cutoff + $low
global graph_high=$cutoff + $high
qui density_parametric_compute 
global winsor=0    
global trim=1      
global myvar  "trade_custom" 
global myvar_name "Trade Participation"
qui causal_est
global winsor=1   
global trim=0      
global myvar  "tfpq" 
global myvar_name "Productivity"
qui causal_est
qui causal_2sls_est



/***********Table C2************/
set matsize 6000
set seed 1234
global draw_fig         =0
global nboot            =500
global run_bootstrap    =1
global reg_degree       "2nd"          
global fe_status        "fe"            
global bs_method        "bparametric"   
global density          "DP_q"  
global cutoff = 300   

global condition       "group==1 & cic_adj2>=13 & cic_adj2<=42 & process_d==0"   
global name            "East_nonprocess_Manuf"
global location        "East"
global firm            "domestic"  
global firm_name       "Domestic-Owned"
global year_condition  "year==2002"
global year_name       "2002"
global year_note       "in 2002"
global cutoff_note     "(cutoff=300)"
global degree=5
global low=-70 
global high=30 
global graph_low=$cutoff + $low
global graph_high=$cutoff + $high
qui density_data
qui density_parametric_compute 
global winsor=0    
global trim=1      
global myvar  "trade_custom" 
global myvar_name "Trade Participation"
qui causal_est
global winsor=1   
global trim=0      
global myvar  "tfpq" 
global myvar_name "Productivity"
qui causal_est
qui causal_2sls_est



/******Table C4*******/  
set matsize 6000
set seed 1234
global draw_fig         =0
global nboot            =500
global run_bootstrap    =1
global reg_degree       "2nd"          
global fe_status        "fe"            
global bs_method        "bparametric"   
global density          "DP_q"  
global cutoff = 300   

global condition   "group==1 &  cic_adj2>=13 & cic_adj2<=42 & (export_custom==1 | (export_custom==0 & exports_all_d==0))"  
global name        "East_direxp_Manuf"
global location        "East"
global firm            "domestic"  
global firm_name       "Domestic-Owned"
global year_condition  "year==2002"
global year_name       "2002"
global year_note       "in 2002"
global cutoff_note     "(cutoff=300)"
global degree=5
global low=-70 
global high=30 
global graph_low=$cutoff + $low
global graph_high=$cutoff + $high
qui density_data
qui density_parametric_compute 
global winsor=0    
global trim=1      
global myvar  "trade_custom" 
global myvar_name "Trade Participation"
qui causal_est
global winsor=1   
global trim=0      
global myvar  "tfpq" 
global myvar_name "Productivity"
qui causal_est
qui causal_2sls_est




/******Column (2) of Tables 2 & 3******/
set matsize 6000
set seed 1234
global draw_fig         =0
global nboot            =500
global run_bootstrap    =1
global reg_degree       "2nd"          
global fe_status        "fe"            
global bs_method        "bparametric"   
global density          "DP_q"  
global cutoff = 300   

global condition       "group==1 & cic_adj2>=13 & cic_adj2<=42 "  
global name            "East_Manuf"
global location        "East"
global firm            "domestic"  
global firm_name       "Domestic-Owned"
global year_condition  "year==2000"
global year_name       "2000"
global year_note       "in 2000"
global cutoff_note     "(cutoff=500 for SOE, =850 for PIE)"
global degree=3
global low=-30
global high=30
global graph_low=$cutoff + $low
global graph_high=$cutoff + $high
qui density_data
qui density_parametric_compute 
global winsor=0    
global trim=1      
global myvar  "trade_custom" 
global myvar_name "Trade Participation"
qui causal_est
global winsor=1   
global trim=0      
global myvar  "tfpq" 
global myvar_name "Productivity"
qui causal_est




/******Column (3) of Tables 2 & 3******/
set matsize 6000
set seed 1234
global draw_fig         =0
global nboot            =500
global run_bootstrap    =1
global reg_degree       "2nd"          
global fe_status        "fe"            
global bs_method        "bparametric"   
global density          "DP_q"  

global cutoff = 300   
global condition       "group==1 & cic_adj2>=13 & cic_adj2<=42 "  
global name            "East_Manuf"
global location        "East"
global firm            "foreign"  
global firm_name       "Foreign-Invested"
global year_condition  "year==2002"
global year_name       "2002"
global year_note       "in 2002"
global cutoff_note     "(no cutoff)"
global degree=4
global low=-60
global high=20
global graph_low=$cutoff + $low
global graph_high=$cutoff + $high
qui density_data
qui do "${rootdir}\code\sub_density_parametric_compute_fie.do"
global winsor=0    
global trim=1      
global myvar  "trade_custom" 
global myvar_name "Trade Participation"
qui causal_est
global winsor=1   
global trim=0      
global myvar  "tfpq" 
global myvar_name "Productivity"
qui causal_est




/******Table G2******/
global firm            "domestic" 
global location        "East"
global year_condition  "year==2002"
global year_name       "2002"
global year_note       "in 2002"
global cutoff_note     "(cutoff=300)"

**Column (1)
global condition       "group==1 & cic_adj2>=13 & cic_adj2<=42 & (age <=7)"
global name            "East_young_manuf"
global firm_name       "Domestic-Owned Young Firms" 
global degree=5
global low=-80
global high=20
global graph_low=$cutoff + $low
global graph_high=$cutoff + $high
qui density_data
qui density_parametric_compute 
global winsor=0    
global trim=1      
global myvar  "trade_custom" 
global myvar_name "Trade Participation"
qui causal_est
global winsor=1   
global trim=0      
global myvar  "tfpq" 
global myvar_name "Productivity"
qui causal_est


**Column (2) 
global condition       "group==1 & cic_adj2>=13 & cic_adj2<=42 & (age>=8 & age~=.)"
global name            "East_old_manuf"
global firm_name       "Domestic-Owned Old Firms" 
global degree=6
global low=-80
global high=10
global graph_low=$cutoff + $low
global graph_high=$cutoff + $high
qui density_data
qui density_parametric_compute
global winsor=0    
global trim=1      
global myvar  "trade_custom" 
global myvar_name "Trade Participation"
qui causal_est
global winsor=1   
global trim=0      
global myvar  "tfpq" 
global myvar_name "Productivity"
qui causal_est


**Column (3) 
global condition       "group==1 & cic_adj2>=13 & cic_adj2<=42 & (private==1)" 
global name            "East_private_manuf"
global firm_name       "Domestic-Owned Private Firms" 
global degree=5
global low=-80
global high=20
global graph_low=$cutoff + $low
global graph_high=$cutoff + $high
qui density_data
qui density_parametric_compute
global winsor=0    
global trim=1      
global myvar  "trade_custom" 
global myvar_name "Trade Participation"
qui causal_est
global winsor=1   
global trim=0      
global myvar  "tfpq" 
global myvar_name "Productivity"
qui causal_est


**Column (4) 
global condition       "group==1 & cic_adj2>=13 & cic_adj2<=42 & (private==0 )" 
global name            "East_SOE_manuf"
global firm_name       "State-Owned Firms" 
global degree=4
global low=-30
global high=50
global graph_low=$cutoff + $low
global graph_high=$cutoff + $high
qui density_data
qui density_parametric_compute
global winsor=0    
global trim=1      
global myvar  "trade_custom" 
global myvar_name "Trade Participation"
qui causal_est
global winsor=1   
global trim=0      
global myvar  "tfpq" 
global myvar_name "Productivity"
qui causal_est



/******Table D1******/
use "${processed_data}/trading_rights_asif.dta", clear
collapse (mean) betal betam betak, by (cic2)



/******Table A2******/
global bin=10
use "${processed_data}/trading_rights_asif.dta", clear
gen capital_norm=RC  //registered capital; unit 10K RMB
gen tinctobin= capital_norm/${bin}
gen incbinaux=floor(tinctobin) 
gen incbin${binname}=incbinaux*${bin}
drop  tinctobin   incbinaux

tabstat tfpq if year==2002 & group==1 & domestic==1 &  cic_adj2>=13 & cic_adj2<=42  & incbin${bin}>= 80 & incbin${bin}<300 & trade_custom==1, stat (mean p25 p50 p75 N)
tabstat tfpq if year==2002 & group==1 & domestic==1 &  cic_adj2>=13 & cic_adj2<=42  & incbin${bin}>= 80 & incbin${bin}<300 & trade_custom==0, stat (mean p25 p50 p75 N)

tabstat tfpq if year==2002 & group==1 & domestic==1 &  cic_adj2>=13 & cic_adj2<=42  & incbin${bin}<=470 & incbin${bin}>=300 & trade_custom==1, stat (mean p25 p50 p75 N)
tabstat tfpq if year==2002 & group==1 & domestic==1 &  cic_adj2>=13 & cic_adj2<=42  & incbin${bin}<=470 & incbin${bin}>=300 & trade_custom==0, stat (mean p25 p50 p75 N)



/******Table A6******/
global condition       "group==1 & cic_adj2>=13 & cic_adj2<=42 "  
global name            "East_Manuf"
global location        "East"
global firm            "domestic"  
qui run "${rootdir}\code\sub_dif.do"
